﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace EpubFactory.Common
{
    public class AppPreference
    {
        const string FileName = "robot-app.xml";
        public static readonly string DefaultTempDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        public static readonly CoverOption DefaultCoverOption = new CoverOption()
        {
            Size = new System.Drawing.Size(600, 800),
            Title = new CoverOption.Item()
            {
                BgColor = Color.FromArgb(212, 220, 243),
                ForeColor = Color.FromArgb(0, 68, 139),
                SimpleFont = new CoverOption.SimpleFont() { Family = SystemFonts.DefaultFont.FontFamily.Name, Size = 50 }
            },
            Author = new CoverOption.Item()
            {
                BgColor = Color.FromArgb(130, 159, 227),
                ForeColor = Color.FromArgb(3, 50, 92),
                SimpleFont = new CoverOption.SimpleFont() { Family = SystemFonts.DefaultFont.FontFamily.Name, Size = 40 }
            }
        };
        public bool EnableRequestCache { get; set; }
        public const string DefaultOutputDir = ".";
        public string TempDir { get; set; }

        public string OutputDir { get; set; }
        public int ParallelismDegree { get; set; }
        public BookFormat OutputFormatters { get; set; }
        public CoverOption CoverOption { get; set; }
        static void DeleteFile(string filePath)
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
        }
        private AppPreference()
        {
            OutputDir = DefaultOutputDir;
            TempDir = DefaultTempDir;
            CoverOption = DefaultCoverOption;
            OutputFormatters = BookFormat.Epub201;
        }

        public static AppPreference Load()
        {
            if (_instance == null)
            {
                _instance = XmlPreference.Load<AppPreference>(FileName);
            }
            if (_instance == null)
            {
                _instance = new AppPreference();
            }
            return _instance;
        }
        public static void Save()
        {
            XmlPreference.Save<AppPreference>(_instance, FileName);
        }
        private static AppPreference _instance;
    }
    public class XmlPreference
    {
        public static T Load<T>(string fileName)
        {
            T instance = default(T);
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var filePath = Path.Combine(path, fileName);
            if (File.Exists(filePath))
            {
                using (TextReader reader = new StreamReader(filePath))
                {
                    try
                    {
                        instance = (T)serializer.Deserialize(reader);
                    }
                    catch (XmlException)
                    {

                    }
                    catch (InvalidOperationException)
                    {

                    }
                    reader.Close();
                }
            }
            return instance;
        }
        public static void DeleteFile(string filePath)
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
        }
        public static void Save<T>(T t, string fileName)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var filePath = Path.Combine(path, fileName);
            DeleteFile(filePath);
            using (TextWriter writer = new StreamWriter(filePath))
            {
                serializer.Serialize(writer, t);
                writer.Close();
            }
        }
    }
    public class UrlItem
    {
        public string Href { get; set; }
        public string Title { get; set; }
        public string Tag { get; set; }
    }
    public class UserPreference
    {
        const string FileName = "lib99-user.xml";
        private UserPreference()
        {
            UrlHistories = new List<UrlItem>();
        }
        public List<UrlItem> UrlHistories { get; private set; }

        public void AddUrl(string url, string tag, string title = null)
        {
            var one = UrlHistories.FirstOrDefault(g => g.Href == url && g.Tag == tag);
            if (one == null)
            {
                one = new UrlItem() { Href = url, Tag = tag, Title = title };
                UrlHistories.Insert(0, one);
            }
            else
            {
                one.Title = title;
                UrlHistories.Remove(one);
                UrlHistories.Insert(0, one);
            }
            if (UrlHistories.Count > 20)
            {
                UrlHistories.RemoveAt(UrlHistories.Count - 1);
            }
            Save();
        }
        public static UserPreference Load()
        {
            if (_instance == null)
            {
                _instance = XmlPreference.Load<UserPreference>(FileName);
            }
            if (_instance == null)
            {
                _instance = new UserPreference();
            }
            return _instance;
        }
        static void DeleteFile(string filePath)
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
        }
        public static void Save()
        {
            XmlPreference.Save<UserPreference>(_instance, FileName);
        }
        private static UserPreference _instance;
    }
}
